package cz.cvut.felk.via.sm.rest.controllers;

import java.util.HashMap;

import javax.annotation.PostConstruct;

import org.datanucleus.sco.backed.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cv.cvut.felk.via.sm.services.UserDummyService;
import cz.cvut.felk.via.sm.exceptions.ResourceNotFoundException;
import cz.cvut.felk.via.sm.model.User;
@Controller
public class UserJsonController {
	UserDummyService userDummyService;
	
	public void setUserDummyService(UserDummyService userDummyService) {
		this.userDummyService = userDummyService;
	}
	@RequestMapping(value="/userlogin/{hash}", method=RequestMethod.GET)
	@ResponseBody
	public String Jsonlogin (@PathVariable String hash)throws ResourceNotFoundException{
		return userDummyService.login(hash);
	}
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public String loginget(Model model){
		model.addAttribute(new User());
		return "user/login";
	}
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String loginpost(User user){
		String result =userDummyService.login(user.getUsername()+"-"+user.getPassword());
		if(result.equals("KO")){
			return "user/login";
		}else{
			return "redirect:/subject";
		}
		
	}
	
}
